package com.wechat.order.controller.UserController;

import com.lly835.bestpay.rest.type.Get;
import com.wechat.order.enums.ResultEnum;
import com.wechat.order.exception.SellException;
import com.wechat.order.form.UserForm;
import com.wechat.order.service.UserService.UserService;
import com.wechat.order.util.ResultVOUtil;
import com.wechat.order.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by zzy on 2017/12/27.
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/rejistIndex")
    public ModelAndView rejistIndex(){
        return new ModelAndView("user/rejister");
    }
    @PostMapping("/rejist.json")
    public ResultVO rejist(@Valid UserForm userForm,BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            log.error("[创建用户] 参数不正确,orderForm={}",userForm);
            throw  new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        userService.register(userForm);
        return ResultVOUtil.success();
    }
}
